package com.tcxhb.job.core.convert;

/**
 * @Description:
 * @Auther: tcxhb
 * @Date: 2023/11/11
 */


import com.tcxhb.job.dao.dataobject.JobInfoDO;
import com.tcxhb.job.dao.dataobject.JobInstanceDO;
import com.tcxhb.job.common.constants.enums.ExecuteStatus;
import com.tcxhb.job.common.constants.enums.ScheduleStatus;
import com.tcxhb.job.common.model.job.RunStrategy;
import org.mapstruct.Mapper;

import java.util.Date;

@Mapper(componentModel = "spring")
public interface JobConvert {
    /**
     * @param jobDO
     * @return
     */
    default JobInstanceDO toInstance(JobInfoDO jobDO, Integer shardingNo) {
        JobInstanceDO instanceDO = new JobInstanceDO();
        instanceDO.setAppName(jobDO.getAppName());
        instanceDO.setJobId(jobDO.getId());
        instanceDO.setJobParams(jobDO.getJobParams());
        //返回分区
        instanceDO.setRunStrategy(copy(jobDO.getRunStrategy()));
        instanceDO.getRunStrategy().setShardingNo(shardingNo);
        instanceDO.setScheduleStatus(ScheduleStatus.INIT.getType());
        instanceDO.setExecuteStatus(ExecuteStatus.INIT.getType());
        instanceDO.setExecuteTime(new Date());
        //需要重试的次数
        instanceDO.setFailCount(0);
        //是否需要重试标志,数据库索引
        RunStrategy run = jobDO.getRunStrategy();
        Integer retryFlag = run.getRetryCount() > 0 ? 1 : 0;
        instanceDO.setRetryFlag(retryFlag);
        return instanceDO;
    }

    public RunStrategy copy(RunStrategy strategy);
}
